package com.situ.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.context.BaseContext;
import com.situ.domain.entity.TAddress;
import com.situ.domain.entity.TDictDistrict;
import com.situ.domain.dto.AddressDTO;
import com.situ.domain.dto.UpdateAddressDTO;
import com.situ.mapper.TAddressMapper;
import com.situ.mapper.TDictDistrictMapper;
import com.situ.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AddressServiceImpl extends ServiceImpl<TAddressMapper, TAddress> implements AddressService {

    private final TDictDistrictMapper dictDistrictMapper;

    /**
     * 添加地址
     * @param addressDTO
     * @param currentId
     */
    @Override
    public void addAddress(AddressDTO addressDTO, Integer currentId) {
        TAddress address = new TAddress();
        List<TDictDistrict> areaName = dictDistrictMapper.getAreaName(
                addressDTO.getProvinceCode(),
                addressDTO.getCityCode(),
                addressDTO.getAreaCode()
        );
        BeanUtil.copyProperties(addressDTO,address);
        address.setProvinceName(areaName.get(0).getName());
        address.setCityName(areaName.get(1).getName());
        address.setAreaName(areaName.get(2).getName());
        // 判断当前用户是否已经有默认地址
        Long count = this.lambdaQuery()
                .eq(TAddress::getUid, currentId)
                .eq(TAddress::getIsDefault, 1)
                .count();
        if (count == 0) {
            address.setIsDefault(1);
        }else {
            address.setIsDefault(0);
        }
        address.setCreatedTime(new Date());
        address.setCreatedUser(currentId.toString());
        address.setUid(currentId);
        this.save(address);
    }


    /**
     * 设置默认地址
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefaultAddress(Integer id) {
        Integer currentId = BaseContext.getCurrentId();
        TAddress address = this.lambdaQuery()
                .eq(TAddress::getUid, currentId)
                .eq(TAddress::getIsDefault, 1)
                .one();
        if (address != null) {
            address.setIsDefault(0);
            this.updateById(address);
        }
        this.lambdaUpdate()
                .eq(TAddress::getUid,currentId)
                .eq(TAddress::getAid,id)
                .set(TAddress::getIsDefault,1)
                .update();
    }

    /**
     * 修改地址
     * @param addressDTO
     */
    @Override
    public void updateAddress(UpdateAddressDTO addressDTO) {
        List<TDictDistrict> areaName = dictDistrictMapper.getAreaName(
                addressDTO.getProvinceCode(),
                addressDTO.getCityCode(),
                addressDTO.getAreaCode()
        );
        this.lambdaUpdate()
                .eq(TAddress::getAid,addressDTO.getAid())
                .set(addressDTO.getName() != null,TAddress::getName,addressDTO.getName())
                .set(addressDTO.getPhone() != null,TAddress::getPhone,addressDTO.getPhone())
                .set(addressDTO.getTel() != null,TAddress::getTel,addressDTO.getTel())
                .set(addressDTO.getAddress() != null,TAddress::getAddress,addressDTO.getAddress())
                .set(addressDTO.getTag() != null,TAddress::getTag,addressDTO.getTag())
                .set(addressDTO.getProvinceCode() != null,TAddress::getProvinceName,areaName.get(0).getName())
                .set(addressDTO.getCityCode() != null,TAddress::getCityName,areaName.get(1).getName())
                .set(addressDTO.getAreaCode() != null,TAddress::getAreaName,areaName.get(2).getName())
                .set(addressDTO.getZip() != null,TAddress::getZip,addressDTO.getZip())
                .set(addressDTO.getProvinceCode() != null,TAddress::getProvinceCode,addressDTO.getProvinceCode())
                .set(addressDTO.getCityCode() != null,TAddress::getCityCode,addressDTO.getCityCode())
                .set(addressDTO.getAreaCode() != null,TAddress::getAreaCode,addressDTO.getAreaCode())
                .set(TAddress::getModifiedTime,new Date())
                .set(TAddress::getModifiedUser,BaseContext.getCurrentId().toString())
                .update();
    }

}
